﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class TDStateManController : Controller
    {
        //
        // GET: /TDStateMan/

        public JsonResult GetPagingGrid(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from dt in context.DSTrinhDoQLNN
                            join ll in context.DSQLNhaNuoc on dt.IDQLNhaNuoc equals ll.ID
                            where dt.IDCanBo == id
                            select new { ll.Ten, dt.TuNgay, dt.DenNgay, dt.IDQLNhaNuoc, dt.ID }).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.IDQLNhaNuoc,
                                 customer.Ten,
                                 // customer.IDCanBo,
                                 customer.TuNgay,
                                 customer.DenNgay
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index(int id = 0)
        {
            List<TrinhDoQLNN> dsqhgd = TrinhDoQLNNModels.GetTrinhDoQLNNTheoCanBo(id);
            ViewBag.DSTrinhDoQLNN = dsqhgd;
            CanBo cb = CanBoModels.GetChiTietCanBo(id);
            ChucVu cv = PosModels.GetChiTietDSChucVu((int)cb.ChucVuHienTai);
            PhongBan pb = PhongBanModels.GetChiTietPhongBan(cb.IDPhongBan);
            ViewBag.ChucVu = cv;
            ViewBag.PhongBan = pb;
            ViewBag.ChiTietCanBo = cb;
            return View(dsqhgd);
        }
        public ActionResult Add(int id = 0)
        {
            List<QLNhaNuoc> dsll = QLNNModels.GetDSQLNN();
            ViewBag.DSQLNN = dsll;
            ViewBag.Id = id;
            return View();
        }
        [HttpPost]
        public ActionResult Add(FormCollection form, int id)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
            TrinhDoQLNN themKyLuat = new TrinhDoQLNN();

            themKyLuat.IDQLNhaNuoc = Convert.ToInt32(form["IDQLNhaNuoc"]);
            themKyLuat.TuNgay = DateTime.Parse(form["TuNgay"],ci.DateTimeFormat);
            themKyLuat.DenNgay = DateTime.Parse(form["DenNgay"],ci.DateTimeFormat);


            themKyLuat.IDCanBo = id;
            //themQHGD.IDCanBo = id;

            TrinhDoQLNNModels.Add(themKyLuat);
            //return RedirectToAction("Index");
            return RedirectToAction("Index", "TDStateMan", new { id = themKyLuat.IDCanBo });
        }

        public ActionResult Update(int id)
        {
            List<QLNhaNuoc> dsll = QLNNModels.GetDSQLNN();
            ViewBag.DSQLNN = dsll;

            TrinhDoQLNN listQHGD = TrinhDoQLNNModels.GetTrinhDoQLNN(id);
            ViewBag.DSTrinhDoQLNN = listQHGD;
            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection form, int id)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
            TrinhDoQLNN s = TrinhDoQLNNModels.GetTrinhDoQLNN(id);
            //s.IDCanBo = Convert.ToInt32(form["IDCanBo"]);
            s.IDQLNhaNuoc = Convert.ToInt32(form["IDQLNhaNuoc"]);
            s.TuNgay = DateTime.Parse(form["TuNgay"],ci.DateTimeFormat);
            s.DenNgay = DateTime.Parse(form["DenNgay"],ci.DateTimeFormat);


            TrinhDoQLNNModels.Update(s);
            return RedirectToAction("Index", "TDStateMan", new { id = s.IDCanBo });
        }
        public ActionResult Delete(int id)
        {
            TrinhDoQLNN s = TrinhDoQLNNModels.GetTrinhDoQLNN(id);
            TrinhDoQLNNModels.Delete(id);

            return RedirectToAction("Index", "TDStateMan", new { id = s.IDCanBo });
        }

    }
}
